<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Implicit conversion - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../experimental/worked-example/source.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/outcome.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Implicit conversion</h1></div></div></div>
  <p>Back in <a href="../../experimental/worked-example/value_type.html">The payload</a>, we
mentioned that there was no default implicit conversion of <code>file_io_error</code>
(<code>status_code&lt;_file_io_error_domain&gt;</code>) to <code>error</code>, as <code>error</code> is too small
to hold <code>_file_io_error_domain::value_type</code>.</p>

<p>We can tell the framework about available implicit conversions by defining
an ADL discovered free function <code>make_status_code()</code> which takes our
custom status code as input, and returns an <code>error</code>:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// Now tell `error` how it can implicitly construct from `file_io_error`.
</span><span class="c1">// This is done by us defining a free function called `make_status_code()`
</span><span class="c1">// which is discovered using ADL. `error` is an alias to the refinement
</span><span class="c1">// `status_code&lt;erased&lt;intptr_t&gt;&gt;` which is a status code whose value type
</span><span class="c1">// has been erased into an `intptr_t`. `status_code&lt;erased&lt;intptr_t&gt;&gt;`
</span><span class="c1">// (i.e. `error`) are move bitcopying (P1029) i.e. they are move-only
</span><span class="c1">// types whose move operation is defined to leave the source in the same
</span><span class="c1">// representation as a default constructed instance, and for whose
</span><span class="c1">// non-trivial destructor when called upon a default constructed instance
</span><span class="c1">// is guaranteed to do nothing.
</span><span class="c1"></span><span class="kr">inline</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">system_code</span> <span class="n">make_status_code</span><span class="p">(</span><span class="n">file_io_error</span> <span class="n">v</span><span class="p">)</span>
<span class="p">{</span>
  <span class="c1">// `make_status_code_ptr()` dynamically allocates memory to store an
</span><span class="c1"></span>  <span class="c1">// instance of `file_io_error`, then returns a status code whose domain
</span><span class="c1"></span>  <span class="c1">// specifies that its value type is a pointer to `file_io_error`. The
</span><span class="c1"></span>  <span class="c1">// domain is a templated instance which indirects all observers of the
</span><span class="c1"></span>  <span class="c1">// status code to the pointed-to status code.
</span><span class="c1"></span>  <span class="c1">//
</span><span class="c1"></span>  <span class="c1">// Note that the status code returned&#39;s value type is a pointer, which
</span><span class="c1"></span>  <span class="c1">// by definition fits into `intptr_t` and is trivially copyable.
</span><span class="c1"></span>  <span class="c1">// Therefore `system_code` (which is also a type alias to
</span><span class="c1"></span>  <span class="c1">// `status_code&lt;erased&lt;intptr_t&gt;&gt;`) is happy to implicitly construct
</span><span class="c1"></span>  <span class="c1">// from the status code returned by `make_status_code_ptr()`.
</span><span class="c1"></span>  <span class="k">return</span> <span class="n">make_status_code_ptr</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">v</span><span class="p">));</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/experimental_status_code.cpp#L152" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>We are now ready to use Experimental Outcome!</p>


        </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../experimental/worked-example/source.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/outcome.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
